package com.example.tianxi.yuexiuhhpatient;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class OnlineTestActivity extends Activity {


    AdapterViewFlipper flipper;//轮播的题目

    //题目信息
    List<JSONObject> lists;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_test);

        initView();
    }

    public void initView(){

        TextView back = (TextView)findViewById(R.id.back);
        back.setText("返回");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView title = (TextView)findViewById(R.id.title);
        title.setText("测试题目");

        TextView action = (TextView)findViewById(R.id.action);
        action.setText("提交");
        action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(OnlineTestActivity.this,"提交答案",Toast.LENGTH_LONG).show();

                new AlertDialog.Builder(OnlineTestActivity.this)
                        .setIcon(null)
                        .setTitle("确认提交答案吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create()
                        .show();
            }
        });


        initData();

        flipper=(AdapterViewFlipper)findViewById(R.id.flipper);
        //创建一个BaseAdapter对象，该对象负责提供Gallery所显示的列表项
        BaseAdapter adapter=new BaseAdapter()
        {

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return lists.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
            //该方法返回的View代表了每个列表项
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                if (convertView == null) {
                    LayoutInflater inflater = LayoutInflater.from(OnlineTestActivity.this);
                    convertView = inflater.inflate(R.layout.activity_online_ask, null);
                }

                TextView title = (TextView)convertView.findViewById(R.id.title);
                RadioButton radioButton1 = (RadioButton)convertView.findViewById(R.id.radio1);
                RadioButton radioButton2 = (RadioButton)convertView.findViewById(R.id.radio2);
                RadioButton radioButton3 = (RadioButton)convertView.findViewById(R.id.radio3);
                RadioButton radioButton4 = (RadioButton)convertView.findViewById(R.id.radio4);

                JSONObject object = lists.get(position);
                try {
                    title.setText(String.valueOf(position+1)+". "+object.getString("title"));
                    radioButton1.setText(object.getString("select1"));
                    radioButton2.setText(object.getString("select2"));
                    radioButton3.setText(object.getString("select3"));
                    radioButton4.setText(object.getString("select4"));
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return convertView;
            }
        };
        flipper.setAdapter(adapter);


    }

    public void initData(){
        lists = new ArrayList<JSONObject>();
        JSONObject object = new JSONObject();
        try{
            object.put("title","请问你最近有参加运动吗，运动量是多大呢，呼吸有没有很急促，喘不上来的感觉有没有？");
            object.put("select1","A. 几乎没有");
            object.put("select2","B. 一般");
            object.put("select3","C. 很多");
            object.put("select4","D. 非常多");
        }catch (JSONException e){

        }
        lists.add(object);
        lists.add(object);
        lists.add(object);
        lists.add(object);
        lists.add(object);
        lists.add(object);
        lists.add(object);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_online_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void prev(View source)
    {
        //显示上一个组件
        flipper.showPrevious();
        //停止自动播放
//        flipper.stopFlipping();
    }

    public void next(View source)
    {
        //显示一个组件
        flipper.showNext();
        //停止自动播放
//        flipper.stopFlipping();
    }

}
